package com.ebarlas.fractal.compile;

import java.util.Collection;

import com.ebarlas.fractal.util.ArrayStack;
import com.ebarlas.fractal.util.Complex;

public class Interpreter {
    
    private Interpreter() {}
    
    public static int maxStack(StackCommand[] commands) {
        int stackSize = 0;
        int maxStack = 0;
        for(StackCommand command: commands) {
            stackSize -= command.consumed();
            stackSize += command.produced();
            if(stackSize > maxStack) {
                maxStack = stackSize;
            }
        }
        return maxStack;
    }    

    public static int maxStack(Collection<StackCommand> commands) {
        int stackSize = 0;
        int maxStack = 0;
        for(StackCommand command: commands) {
            stackSize -= command.consumed();
            stackSize += command.produced();
            if(stackSize > maxStack) {
                maxStack = stackSize;
            }
        }
        return maxStack;
    }
    
    public static Complex interpret(Collection<StackCommand> commands) {
        int maxStack = maxStack(commands);
        ArrayStack<Complex> stack = new ArrayStack<Complex>(maxStack);
        for(StackCommand command : commands) {
            command.interpret(stack);
        }
        return stack.pop();
    }
    
    public static Complex interpret(StackCommand[] commands, ArrayStack<Complex> stack) {
        stack.clear();
        for(StackCommand command : commands) {
            command.interpret(stack);
        }
        return stack.pop();
    }    
    
    public static Complex interpret(Collection<StackCommand> commands, ArrayStack<Complex> stack) {
        stack.clear();
        for(StackCommand command : commands) {
            command.interpret(stack);
        }
        return stack.pop();
    }    
    
}
